import {
  getOtherBillList,
  getSixBillList,
  getThreeBillList,
  getETFList,
  getETFFilterList,
  reCreateOrder,
  sellPutPlanList,
  getUpsOptionList,
  getRoyaltiesList,
  getFilterList,
  getEarningDateChangeRateList,
  getTextBoxList,
  getImpliedVolatility,
  getKlineStatistic,
} from '../../../../services/option_service';

export default {
  namespace: 'rule',
  state: {
    threedata: {
      code: 0,
      message: '',
      data: [],
    },
    sixdata: {
      code: 0,
      message: '',
      data: [],
    },
    otherdata: {
      code: 0,
      message: '',
      data: [],
    },

    data: {
      list: [],
      pagination: {},
    },
    data2: {
      list: {
        maxValue: '',
        meanValue: '',
        meanValueOfFall: '',
        meanValueOfRise: '',
        medianValue: '',
        medianValueOfFall: '',
        medianValueOfRise: '',
        minValue: '',
        nextEarningsDate: '',
        winRate: '',
        marketValue: '',
        fundEarningDateChangeRateModels: [],
      },
      pagination: {},
    },
    filterData: {
      list: [],
    },

    timeLineChartData: {
      code: '',
      message: '',
      list: [],
    },

    klineStatisticData: {
      code: '',
      message: '',
      list: {
        values: [],
        x: '',
      },
    },
  },

  effects: {
    *GetETFList({ payload }, { call, put }) {
      const response = yield call(getETFList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *GetETFFilterList({ payload }, { call, put }) {
      const response = yield call(getETFFilterList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFilter',
        payload: response,
      });
    },

    *GetRoyaltiesList({ payload }, { call, put }) {
      const response = yield call(getRoyaltiesList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *GetFilterList({ payload }, { call, put }) {
      const response = yield call(getFilterList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFilter',
        payload: response,
      });
    },
    *GetEarningDateChangeRateList({ payload, callback }, { call, put }) {
      const response = yield call(getEarningDateChangeRateList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveList2',
        payload: response,
      });
      if (callback) callback(response);
    },
    *GetTextBoxList({ payload, callback }, { call, put }) {
      const response = yield call(getTextBoxList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFilter',
        payload: response,
      });
      if (callback) callback(response);
    },
    *GetUpsOptionList({ payload, callback }, { call, put }) {
      const response = yield call(getUpsOptionList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    *SellPutPlanList({ payload, callback }, { call, put }) {
      const response = yield call(sellPutPlanList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    *ReCreateOrder({ payload, callback }, { call, put }) {
      const response = yield call(reCreateOrder, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },

    *GetImpliedVolatility({ payload, callback }, { call, put }) {
      const response = yield call(getImpliedVolatility, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveTimeLineChartData',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetKlineStatistic({ payload, callback }, { call, put }) {
      const response = yield call(getKlineStatistic, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (callback) callback(response);
      yield put({
        type: 'saveKlineStatisticData',
        payload: response,
      });
    },

    *GetThreeBillList({ payload, callback }, { call, put }) {
      const response = yield call(getThreeBillList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveThreeList',
        payload: response,
      });
      if (callback) callback(response);
    },
    *GetSixBillList({ payload, callback }, { call, put }) {
      const response = yield call(getSixBillList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveSixList',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetOtherBillList({ payload, callback }, { call, put }) {
      const response = yield call(getOtherBillList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveOtherList',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },

    saveFilter(state, action) {
      return {
        ...state,
        filterData: action.payload,
      };
    },
    saveList2(state, action) {
      return {
        ...state,
        data2: action.payload,
      };
    },

    saveTimeLineChartData(state, action) {
      return {
        ...state,
        timeLineChartData: action.payload,
      };
    },

    saveKlineStatisticData(state, action) {
      return {
        ...state,
        klineStatisticData: action.payload,
      };
    },

    saveThreeList(state, action) {
      return {
        ...state,
        threedata: action.payload,
      };
    },
    saveSixList(state, action) {
      return {
        ...state,
        sixdata: action.payload,
      };
    },
    saveOtherList(state, action) {
      return {
        ...state,
        otherdata: action.payload,
      };
    },
  },
};
